import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
import {userStore} from '../../store/index'
const db = wx.cloud.database()
const app = getApp()
Page({
  data: {
    username:'',
    password:'',
    regUsername:'',
    regPassword:'',
    regRePassword:''
  },
  onLoad: function (options) {

  },

  //======================返回==============================
  handleBackBtnClick(){
    wx.navigateBack()
  },
  //======================登录==============================
  handleInput(event){
    let type = event.currentTarget.id
    this.setData({
      [type]: event.detail.value
    })
  },
  login(){
   var that = this
   let{ username ,password }= this.data
   if(!username){
     Dialog.alert({
       message:'用户名不能为空'
     })
     return;
   }
   if(!password){
     Dialog.alert({
       message:'密码不能为空'
     })
     return;
   }
   db.collection('user').where({
     username:username,
     password:password
   }).get({
      success(res){
        console.log(res.data);
        if(res.data[0]){
          wx.switchTab({
            url: '/pages/home-profile/index',
          })
          const id = res.data[0]._id
          userStore.setState("id",id)
          const nickname = res.data[0].nickname
          userStore.setState("nickname",nickname)
          const username = res.data[0].username
          userStore.setState("username",username)
          const password = res.data[0].password
          userStore.setState("password",password)
          const songlist = res.data[0].songlist
          userStore.setState("songlist",songlist)
          const avatarUrl = res.data[0].avatarUrl
          userStore.setState("avatarUrl",avatarUrl)
          const sex = res.data[0].sex
          userStore.setState("sex",sex)
          db.collection('user').doc(id).update({
            data:{
              isLogin: 'true'
            },
          })
          userStore.setState("isLogin","true")
        }else{
          Dialog.alert({
            message:'用户名或者密码错误！'
          })
          that.setData({
            password:''         
          })
        }   
      },
   })
      
  },

//===========================注册============================
  handleRegInput(event){
    let type = event.currentTarget.id
    this.setData({
     [type]:event.detail.value
    })
  },

  register(){
    var that = this
    let{regPassword,regRePassword,regUsername} = this.data
    if(!regUsername){
      Dialog.alert({
        message:'用户名不能为空'
      })
      return;
    }
    db.collection('user').where({
      username:regUsername
    })
    .get({
      success(res){
        if(res.data[0]){
          Dialog.alert({
            message:'已经存在该用户名'
          })
          return;
        }        
      }
    })

    if(!regPassword){
      Dialog.alert({
        message:'密码不能为空'
      })
      return;
    }
    if(!regRePassword){
      Dialog.alert({
        message:'请再次输入密码'
      })
      return;
    }
    if(regRePassword !== regPassword){
      Dialog.alert({
        message:'两次密码不一致'
      })
      return;
    }

    db.collection('user').add({
      data:{
        sex:"未知",
        songlist:'',
        isLogin:'false',
        nickname:'默认昵称',
        password:regPassword,
        username:regUsername,
        avatarUrl:'cloud://cloud1-4g1pg99vd7e6da1b.636c-cloud1-4g1pg99vd7e6da1b-1308229179/default.png'
      },
      success(){
        that.setData({
          regPassword:'',
          regRePassword:''
        })
        Dialog.alert({
          message:'注册成功'
        })
        return;
      }
    })
    
    
  }


    
  

})